//
//  HMReleaseHistory.swift
//  HealthMonitoring
//
//  Created by YTYanK on 2018/10/22.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

/// 发布历史cell
class HMReleaseHistoryCell: HMTableViewCell, UITextViewDelegate {
    
    var rhCellId = ""
    // 时间
    let dateLabel = UILabel()
    // 费用
    let priceLabel = UILabel()
    // 标记1
    let regionLabel = UILabel()
    let officeLabel = UILabel()
    let levelLabel = UILabel()
    
    let textView = UITextView()
    
    
    private lazy var consultBtn:UIButton = {
        let _btn = UIButton()
        _btn.setTitle("再次咨询", for: .normal)
        _btn.setTitleColor(UIColor.white, for: .normal)
        _btn.backgroundColor = configure.shareConfigure.HMColor1
        return _btn
    }()
    
    private lazy var des: UILabel = {
         let _des = UILabel()
        _des.text = "病情描述"
        return _des
    }()
    
    override func setupViews(view: UIView) {
        view.addSubview(dateLabel)
        view.addSubview(priceLabel)
        view.addSubview(regionLabel)
        view.addSubview(officeLabel)
        view.addSubview(levelLabel)
        view.addSubview(textView)
        view.addSubview(des)
        view.addSubview(consultBtn)
        
        
        dateLabel.topAlign(view: view, predicate: "8")
        dateLabel.text = "2018-10-19"
        dateLabel.heightConstrain(predicate: "30")
        dateLabel.leadingAlign(view: view)
        
        
        priceLabel.topAlign(view: view, predicate: "8")
        priceLabel.text = "费用15元/次"
        priceLabel.heightConstrain(predicate: "30")
        priceLabel.leadingHorizontally(view: dateLabel, predicate: "0")
        priceLabel.trailingAlign(view: view)
        
        
        regionLabel.topVertically(view: dateLabel, predicate: "8")
        regionLabel.leadingAlign(view: view)
        regionLabel.text = " 任意 "
        regionLabel.backgroundColor = configure.shareConfigure.HMColor1
        regionLabel.textColor = UIColor.white
        regionLabel.height(view: dateLabel)
        regionLabel.layer.cornerRadius = 4
        regionLabel.layer.masksToBounds = true
        
        
        officeLabel.topAlign(view: regionLabel, predicate: "0")
        officeLabel.leadingHorizontally(view: regionLabel, predicate: "10")
        officeLabel.text = " 任意 "
        officeLabel.textColor = .white
        officeLabel.height(view: dateLabel)
        officeLabel.backgroundColor = configure.shareConfigure.HMColor1
        officeLabel.layer.cornerRadius = 4
        officeLabel.layer.masksToBounds = true
        
        
        levelLabel.topAlign(view: officeLabel, predicate: "0")
        levelLabel.leadingHorizontally(view: officeLabel, predicate: "10")
        levelLabel.text = " 任意 "
        levelLabel.textColor = .white
        levelLabel.height(view: dateLabel)
        levelLabel.backgroundColor = configure.shareConfigure.HMColor1
        levelLabel.layer.cornerRadius = 4
        levelLabel.layer.masksToBounds = true
        

        des.topVertically(view: regionLabel, predicate: "0")
        des.leadingAlign(view: view)
        des.heightConstrain(predicate: "30")
        des.isEnabled = true
        
        
        textView.topVertically(view: des, predicate: "0")
        textView.leadingAlign(view: view)
        textView.layer.borderWidth = 1
        textView.layer.borderColor = UIColor.black.cgColor
        textView.width(view: view, predicate: "*0.7")
        textView.bottomAlign(view: view)
        textView.delegate = self
        textView.font = UIFont.systemFont(ofSize: 15)
        
        
        
        consultBtn.centerYAlign(view: textView)
        consultBtn.layer.cornerRadius = 50
        consultBtn.heightConstrain(predicate: "100")
        consultBtn.widthConstrain(predicate: "100")
        consultBtn.trailingAlign(view: view)
        
    }
   
    override func setJson(json: JSON) {
        rhCellId = json["id"].stringValue
        let str =  json["createdTime"].stringValue
        dateLabel.text = String.chineseTime(frome: str)
        
        priceLabel.text = "费用\(json["cost"].stringValue)元/次"
        officeLabel.text = " \(json["office"].stringValue) "
        levelLabel.text = " \(json["level"].stringValue) "
        regionLabel.text = " \(json["region"].stringValue) "
        textView.text = json["desc"].stringValue
        
        //status     1 邀请咨询，2 等待回应， 3 立即沟通, 4咨询结束, 5 咨询关闭
    }
    
//    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//    return NO;
//    } 
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        return false
    }
    
}


/// 发布历史
class HMReleaseHistory: HMViewController {
    
    var rhJsonData = JSON()
    
    private lazy var  table: UITableView = {
        let _table = UITableView()
        _table.delegate = self
        _table.dataSource = self
        _table.register(HMReleaseHistoryCell.classForCoder(), forCellReuseIdentifier: "HMReleaseHistoryCell")
        return _table
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func setupView() {
        self.title = "发布历史"
        view.backgroundColor = configure.shareConfigure.HMColor2

        view.addSubview(table)
        table.topAlign(view: view, predicate: "0")
        table.leadingAlign(view: view, predicate: "0")
        table.trailingAlign(view: view, predicate: "0")
        table.bottomAlign(view: view, predicate: "0")
    }
    
    
}


extension HMReleaseHistory: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rhJsonData.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 220
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = table.dequeueReusableCell(withIdentifier: "HMReleaseHistoryCell") as! HMReleaseHistoryCell
        cell.setJson(json: rhJsonData[indexPath.row])
       return cell
    }
}
